package com.liyongjian.apidoc.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liyongjian.apidoc.dal.ApiRepository;
import com.liyongjian.apidoc.dal.ModelRepository;
import com.liyongjian.apidoc.dal.ParamRepository;
import com.liyongjian.apidoc.dal.ResRepository;
import com.liyongjian.apidoc.dal.SysRepository;
import com.liyongjian.apidoc.dal.TestRepository;
import com.liyongjian.apidoc.dal.UserRepository;
import com.liyongjian.apidoc.domain.User;
import com.liyongjian.apidoc.domain.enumm.LiveDieState;
import com.liyongjian.apidoc.dto.AjaxResponse;
import com.liyongjian.apidoc.dto.SessionUser;
import com.liyongjian.apidoc.exception.ServerException;



@Controller
@RequestMapping("")
public class LoginController extends BaseController {

    @Autowired
    private SysRepository sysRepository;
    @Autowired
    private ApiRepository apiRepository;
    @Autowired
    private ParamRepository paramRepository;
    @Autowired
    private ResRepository resRepository;
    @Autowired
    private ModelRepository modelRepository;
    @Autowired
    private TestRepository testRepository;
    @Autowired
    private UserRepository userRepository;

    @RequestMapping("login")
    public String login(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        return "login";
    }


    @Transactional
    @ResponseBody
    @RequestMapping("login-login")
    public AjaxResponse login_login(HttpServletRequest request, String userName, String password) {
        AjaxResponse ajax = new AjaxResponse(true);
        User user = userRepository.findByPhone(userName);
        if (user == null || user.state != LiveDieState.LIVE) {
            throw new ServerException("手机号或密码错误");
        }
        SessionUser su = new SessionUser();
        su.setId(user.id);
        su.setName(user.userName);
        saveCurrentUser(request, su);
        return ajax;
    }


    @RequestMapping("reg")
    public String reg(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        return "reg";
    }


    @Transactional
    @ResponseBody
    @RequestMapping("reg-reg")
    public AjaxResponse reg_reg(HttpServletRequest request, String userName, String password) {
        AjaxResponse ajax = new AjaxResponse(true);
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            throw new ServerException("手机号和密码不能为空");
        }
        if (!userName.matches("^[1][3,4,5,7,8][0-9]{9}$")) {
            throw new ServerException("手机号不正确");
        }
        User user = userRepository.findByPhone(userName);
        if (user != null) {
            throw new ServerException("手机号已经存在");
        }
        user = new User();
        user.phone = userName;
        user.password = password;
        user.userName = userName;
        userRepository.save(user);
        return ajax;
    }

    @RequestMapping("logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.logout(request);
        response.sendRedirect(request.getContextPath() + "/login");
    }

}
